package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    @RequiredPermission(name = "角色列表",expression = "role:list")
    public String list(Model model, QueryObject qo){
        model.addAttribute("pageInfo", roleService.query(qo));
        return "/role/list";

    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiredPermission(name = "角色删除",expression = "role:delete")
    public JsonResult delete(Long id){


            if (id != null) {
                roleService.delete(id);
            }
            return new JsonResult();//success 默认为true


    }

    @RequestMapping("/input")
    @RequiredPermission(name = "角色新增/修改页面",expression = "role:input")
    public String input(Long id, Model model){

        model.addAttribute("permissions", permissionService.listAll());

        if (id != null) {
            Role role = roleService.get(id);
            model.addAttribute("role", role);
        }
        return "/role/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "角色新增/修改",expression = "role:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Role role, Long[] ids){

            if (role.getId() != null) {
                roleService.update(role,ids);
            }
            else {
                roleService.save(role,ids);
            }
            return new JsonResult();

    }
}
